#include "mainwindow.h"

#include <QMouseEvent>
#include <QtGui/QGraphicsView>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsPixmapItem>
#include <QApplication>
#include <QGraphicsOpacityEffect>
#include <QDesktopWidget>
#include <QDebug>

#ifdef Q_OS_SYMBIAN
#include <eikenv.h>
#include <coemain.h>
#include <aknappui.h>
#endif



int MainWindow::mViewMode = 0;

int MainWindow::getViewMode()
{
    return mViewMode;
}
#ifdef Q_OS_SYMBIAN
void MainWindow::setPortraitMode()
{
    CAknAppUi *aknAppUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
    if (aknAppUi)
        aknAppUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
}

void MainWindow::setLandscapeMode()
{
    CAknAppUi *aknAppUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
    if (aknAppUi)
        aknAppUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape);
}
#endif

MainWindow::MainWindow(QWidget *parent, int viewMode) :
    QMainWindow(parent),
    mView(0),
    mScene(0)
{
    QPalette pal(palette());
    pal.setBrush(QPalette::Background, QBrush(Qt::black));
    setPalette(pal);
    //this->setWindowOpacity(0.5);

    setMinimumSize(0,0);
    show();
    mSizeAnim = new QPropertyAnimation(this, "geometry");
    mSizeAnim->setDuration(1500);
    //mSizeAnim->start();
    mViewMode = viewMode;
    finishScale();
    connect(mSizeAnim, SIGNAL(finished()), this, SLOT(finishScale()));
}
void MainWindow::finishScale()
{
    this->setWindowOpacity(1);
    viewChanged(mViewMode);
    connect(this, SIGNAL(viewChange(bool)), this, SLOT(viewChanged(bool)));
}
MainWindow::~MainWindow()
{

}
void MainWindow::closeWindow()
{
    setAttribute(Qt::WA_LockPortraitOrientation, true);
#if defined Q_OS_SYMBIAN || defined Q_WS_SIMULATOR
    QRect rt = QApplication::desktop()->screenGeometry();
    mSizeAnim->setStartValue(rt);
    mSizeAnim->setEndValue(QRect(rt.width()/2-74, rt.height()/2-74, 74, 74));
#elif defined Q_WS_WIN
    mSizeAnim->setStartValue(QPoint(640, 360));
    mSizeAnim->setEndValue(QPoint(0,0));
#endif

    this->setWindowOpacity(0.5);

    disconnect(mSizeAnim, SIGNAL(finished()), this, SLOT(finishScale()));
    connect(mSizeAnim, SIGNAL(finished()), QApplication::instance(), SLOT(quit()));
    //mSizeAnim->setDirection(QAbstractAnimation::Backward);
    mSizeAnim->setEasingCurve(QEasingCurve::Linear);
    mSizeAnim->start();
}
void MainWindow::clear()
{
}

void MainWindow::viewChanged(bool viewMode)
{
    mViewMode = (int)viewMode;
    clear();

#if defined Q_WS_WIN
    show();
    if(mViewMode == 0)
    {
        setFixedSize(360,640);
    }
    else if(mViewMode == 1)
    {
        setFixedSize(640,360);
    }

    if(!mScene)
        mScene = new QGraphicsScene(this);
    if(mView)
        mView->reset(mViewMode);
    if(!mView)
        mView = new PicturesView(mScene, this, mViewMode);
    setCentralWidget(mView);
    mView->setGeometry(rect());
    mScene->setSceneRect(mView->rect());
    mView->initPictures();
#endif

#if defined Q_OS_SYMBIAN || defined Q_WS_SIMULATOR
    showFullScreen();

    if(mViewMode == 1)
    {
        //setLandscapeMode();
        setAttribute(Qt::WA_LockLandscapeOrientation, true);
    }
    else if(mViewMode == 0)
    {
        //setPortraitMode();
        setAttribute(Qt::WA_LockPortraitOrientation, true);
    }

    if(!mScene)
        mScene = new QGraphicsScene(this);
    if(mView)
        mView->reset(mViewMode);
    if(!mView)
        mView = new PicturesView(mScene, this, mViewMode);
    setCentralWidget(mView);
    mView->setGeometry(rect());
    mScene->setSceneRect(mView->rect());

    mView->initPictures();
#endif

    QPalette palette;
    palette.setColor(QPalette::Window, Qt::black);
    palette.setColor(QPalette::Background, Qt::black);
    palette.setColor(QPalette::Foreground, Qt::black);
    palette.setColor(QPalette::Base, Qt::black);
    palette.setColor(QPalette::AlternateBase, Qt::black);
    setPalette(palette);
}

void MainWindow::initialize()
{
    if(mView)
        mView->initPictures();
}

void MainWindow::mouseMoveEvent ( QMouseEvent * event )
{
    if(event->buttons() == Qt::LeftButton)
    {
        mMousePos = event->pos();
    }
}

void MainWindow::mousePressEvent ( QMouseEvent * event )
{
    mMousePos = event->pos();
}

void MainWindow::mouseReleaseEvent ( QMouseEvent * event )
{
}
